﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.RM.Bll;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.RM
{
    public partial class FrmShouhuo_2 : Form
    {
        public FrmShouhuo_2(string BillNo, string RowNo, decimal InQty, string DateCode, string Xuehao, string BarCode)
        {
            InitializeComponent();

            _BillNo = BillNo;
            _RowNo = RowNo;
            _InQty = InQty;
            _Xuehao = Xuehao;
            _DateCode = DateCode;
            _BarCode = BarCode;
        }


        string _BillNo = "";
        string _RowNo = "";
        string _PartNumber = "";
        string _DateCode = "";
        string _Xuehao = "";
        decimal _InQty = 0;
        decimal _PlanQty = 0;
        int _OrderNo = 0;
        string _BarCode = "";

        private void FrmRMOut_Load(object sender, EventArgs e)
        {
            InitFrm();

            var sqlRow = " and r.BillNo='{0}' and r.RowNo={1}";
            sqlRow = string.Format(sqlRow, _BillNo, _RowNo);
            var dtBills = Bll_Shouhuo.GetRowInfo(sqlRow);
            if (dtBills.Rows.Count == 0)
            {
                areaMsg.Text = "查无此单号，请确认单号是否正确";
                return;
            }

            _PlanQty = Convert.ToDecimal(dtBills.Rows[0]["PlanQty"].ToString());
            _OrderNo = Convert.ToInt16(dtBills.Rows[0]["OrderNo"].ToString());
            _PartNumber = dtBills.Rows[0]["PartNumber"].ToString();

            var strInfo = "订单详情:\r\n";
            strInfo += "采购订单>" + dtBills.Rows[0]["RelatedBill"].ToString() + "," + _RowNo + "行\r\n";
            strInfo += "物料编号>" + dtBills.Rows[0]["PartNumber"].ToString().Trim() + " " + dtBills.Rows[0]["Version"].ToString() + "\r\n";
            strInfo += "订单数量>" + dtBills.Rows[0]["PlanQty"].ToString() + " " + dtBills.Rows[0]["Unit"].ToString() + "\r\n";
            areaInfo.Text = strInfo;
            areaMsg.Text = "";
        }


        /// <summary>
        /// 判断时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool IsDateTime(string str)
        {
            System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);
            try
            {
                DateTime.ParseExact(str, "yyyyMMdd", format);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 初始化画面
        /// </summary>
        private void InitFrm()
        {
            areaInfo.Text = "";
            areaMsg.Text = "";

            txtXuehao.Text = _Xuehao;
            txtDateCode.Text = _DateCode;
            txtQty.Text = _InQty.ToString();
        }

        private void LockFrm()
        {
            txtXuehao.Enabled = false;
            txtDateCode.Enabled = false;
            txtQty.Enabled = false;
            txtWait.Enabled = false;
            txtASN.Enabled = false;
        }

        private void txtQty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                _InQty = Convert.ToDecimal(txtQty.Text);
            }
            catch
            {
                areaMsg.Text = "收货数量异常";
                txtQty.Focus();
                return;
            }

            if (_InQty == 0)
            {
                areaMsg.Text = "收货数量不能为0";
                txtQty.Focus();
                return;
            }
            if (Convert.ToDecimal(_InQty) > Convert.ToDecimal(_PlanQty))
            {
                areaMsg.Text = "收货数量不能大于计划数量";
                txtQty.Focus();
                return;
            }

            txtDateCode.Focus();
        }

        private void txtDateCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            if (IsDateTime(txtDateCode.Text) == false)
            {
                areaMsg.Text = "DataCode格式错误";
                txtDateCode.Focus();
                return;
            }

            txtWait.Focus();
        }

        private void txtWait_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            if (txtWait.Text.Length == 0)
            {
                areaMsg.Text = "待检位置不能为空";
                txtWait.Focus();
                return;
            }

            txtXuehao.Focus();
        }

        private void txtXuehao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                _InQty = Convert.ToDecimal(txtQty.Text);
            }
            catch
            {
                areaMsg.Text = "收货数量异常";
                txtQty.Focus();
                return;
            }
            if (_InQty == 0)
            {
                areaMsg.Text = "收货数量不能为0";
                txtQty.Focus();
                return;
            }
            if (Convert.ToDecimal(_InQty) > Convert.ToDecimal(_PlanQty))
            {
                areaMsg.Text = "收货数量不能大于计划数量";
                txtQty.Focus();
                return;
            }

            if (IsDateTime(txtDateCode.Text) == false)
            {
                areaMsg.Text = "DataCode格式错误";
                txtDateCode.Focus();
                return;
            }

            if (txtWait.Text.Length == 0)
            {
                areaMsg.Text = "待检位置不能为空";
                txtWait.Focus();
                return;
            }

            txtASN.Focus();
        }

        private void txtASN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                _InQty = Convert.ToDecimal(txtQty.Text);
            }
            catch
            {
                areaMsg.Text = "收货数量异常";
                txtQty.Focus();
                return;
            }

            if (_InQty == 0)
            {
                areaMsg.Text = "收货数量不能为0";
                txtQty.Focus();
                return;
            }
            if (Convert.ToDecimal(_InQty) > Convert.ToDecimal(_PlanQty))
            {
                areaMsg.Text = "收货数量不能大于计划数量";
                txtQty.Focus();
                return;
            }

            if (IsDateTime(txtDateCode.Text) == false)
            {
                areaMsg.Text = "DataCode格式错误";
                txtDateCode.Focus();
                return;
            }

            var dateCode = txtDateCode.Text;

            var chkRet = Bll_Shouhuo.CheckExpiry(_PartNumber, dateCode);
            if (chkRet != "")
            {
                areaMsg.Text = chkRet;
                return;
            }

            if (string.IsNullOrEmpty(txtWait.Text))
            {
                areaMsg.Text = "待检位置不能为空";
                txtWait.Focus();
                return;
            }
            var waitRemark = txtWait.Text;

            var xueh = " ";
            if (string.IsNullOrEmpty(txtXuehao.Text) == false)
            {
                xueh = txtXuehao.Text;
            }

            var asn = " ";
            if (string.IsNullOrEmpty(txtASN.Text) == false)
            {
                asn = txtASN.Text;
            }

            var dRet = MessageBox.Show("是否进行收货？", "消息提示",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            if (dRet == DialogResult.OK)
            {
                var codeContent = _BarCode.Split(',');
                string supplyNo = "";
                if (codeContent.Length > 7)
                {
                    supplyNo = codeContent[7];
                }
                string strSQL = string.Format(@"update wms_T_InStockRMRows set RealQty={0},Updator='{1}',UpdateTime=getdate(),
                    Ext2='{2}',WaitRemark='{6}',ToolRemark='{7}',ASN='{8}',SupplyNo='{9}' 
                    where RowNo='{3}' and BillNo='{4}' and OrderNo='{5}';",
                        _InQty, Utils.uContext.UserNamek__BackingField, dateCode, _RowNo, _BillNo, _OrderNo, waitRemark.ToUpper(), xueh, asn, supplyNo);
                var ret = Utils.NMS.ExcuteSQL(strSQL);
                if (ret == 0)
                {
                    areaMsg.Text = "收货失败";
                    return;
                }

                areaMsg.Text = Bll_Shouhuo.ExcProc(_BillNo, _RowNo, Utils.uContext.UserNamek__BackingField);
                string newBill = "";
                if (areaMsg.Text.StartsWith("N"))
                {
                    Bll_Shouhuo.Bills.Add(_BillNo);
                    newBill = areaMsg.Text.Split(':')[1];
                    LockFrm();
                }
                else
                {
                    return;
                }

                try
                {

                    var sqlRow = " and BillNo='{0}' and RowNo={1}";
                    sqlRow = string.Format(sqlRow, _BillNo, _RowNo);
                    var dtLot = Bll_Shouhuo.GetRowLot(sqlRow);
                    if (dtLot.Rows.Count > 0)
                    {
                        var lotNo = dtLot.Rows[0]["LotNo"].ToString();
                        areaMsg.Text = areaMsg.Text + "," + lotNo;
                    }
                }
                catch
                { }
                var retLog = Bll_Shouhuo.AddLog(newBill, _RowNo, Utils.uContext.UserNamek__BackingField, _BarCode);
                if (retLog > 0)
                {
                    areaMsg.Text = areaMsg.Text + "\r\n打印请求已发送";
                    this.DialogResult = DialogResult.OK;
                    dRet = MessageBox.Show("是否关闭画面？", "消息提示",
                                            MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
                    if (dRet == DialogResult.OK)
                    {
                        this.Close();
                    }
                }
                else
                {
                    areaMsg.Text = areaMsg.Text + "\r\n打印请求发送失败";
                }

                //System.Collections.Hashtable pars = new System.Collections.Hashtable();
                //pars.Add("BillNo", newBill);
                //pars.Add("RowNo", _RowNo);
                //var drUrl = Bll_Shouhuo.GetPrintUrl();
                //if (drUrl.Rows.Count > 0)
                //{
                //    areaMsg.Text = areaMsg.Text + "\r\n" + CIT.WMS.Common.Http.PostWebService(drUrl.Rows[0]["OptValue"].ToString(), drUrl.Rows[0]["OptName"].ToString(), pars);
                //}
                //else
                //{
                //    areaMsg.Text = "参数未配置，无法打印";
                //}
            }
        }
        //END

    }
}